package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: Kris Wu
 * @time: 2020/11/19 22:16
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping(value = "/findPage", method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * 新增权限
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    /**
     * 根据id查询权限数据
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Result findById(Integer id) {
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 根据id更新权限数据
     *
     * @param permission
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result edit(@RequestBody Permission permission) {
        try {
            permissionService.edit(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    /**
     * 根据id删除权限数据
     *
     * @param permissionId
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    public Result deleteById(Integer permissionId) {
        try {
            permissionService.deleteById(permissionId);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    /**
     * 校验权限名称是否重复
     *
     * @return
     */
    @RequestMapping(value = "/checkName", method = RequestMethod.POST)
    public boolean findPermissionByName(@RequestBody Map map) {
        Permission permission = permissionService.findPermissionByName((String) map.get("name"));
        if (map.get("id") != null) {
            return (permission != null && map.get("id") != permission.getId());
        }
        return permission != null;
    }

    /**
     * 校验权限关键字是否重复
     *
     * @return
     */
    @RequestMapping(value = "/checkKeyword", method = RequestMethod.POST)
    public boolean findPermissionByKeyword(@RequestBody Map map) {
        Permission permission = permissionService.findPermissionByKeyword((String) map.get("keyword"));
        if (map.get("id") != null) {
            return (permission != null && map.get("id") != permission.getId());
        }
        return permission != null;
    }

    /**
     * 查询与角色相关联数据
     *
     * @return
     */
    @RequestMapping(value = "/queryRoleById", method = RequestMethod.GET)
    public Result queryRoleById(Integer permissionId) {
        try {
            List<Role> role = permissionService.queryRoleById(permissionId);
            return new Result(true, MessageConstant.QUERY_ROLE2_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE2_FAIL);
        }
    }

}
